/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formatters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import presentation.CurrencyElement;
import presentation.DateElement;
import presentation.LinkElement;
import presentation.PresentationElement;
import presentation.TextElement;

/**
 *
 * @author Alejandro
 */
public class ViewFormatter implements Formatter {

    private String output;

    @Override
    public String format(PresentationElement e) {
        e.acceptView(this);
        return output;
    }

    public void formatText(PresentationElement e) {
        TextElement text = (TextElement) e;
        output = text.getValue();
    }

    public void formatLink(PresentationElement e) {
        LinkElement link = (LinkElement) e;
        StringBuilder sb = new StringBuilder();
        List<LinkElement.Pair> list = (List<LinkElement.Pair>) link.getParam(LinkElement.GET_EXTERNALS);
        for (int i=0; i<list.size(); i++) {
            if ((Boolean) list.get(i).second) {
                sb.append(format((PresentationElement) list.get(i).first));
                sb.append(" ");
            }
        }
        output = sb.toString();
    }

    public void formatCurrency(PresentationElement e) {
        CurrencyElement currency = (CurrencyElement) e;
        output = "$" + currency.getValue();
    }

    public void formatDate(PresentationElement e) {
        try {
            DateElement date = (DateElement) e;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
            Date d = sdf.parse(date.getValue());
            sdf.applyPattern("dd.MM.yyyy");
            output = sdf.format(d);
        } catch (ParseException ex) {
        }
    }
}
